<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>地区管理</title>
    #include("common/header.html")
    <link rel="stylesheet" href="/admin/css/table.css"/>
</head>

<body>
<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <a href="javascript:;" class="layui-btn layui-btn-small" id="add">
            <i class="layui-icon">&#xe608;</i> 添加市
        </a>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>市区列表</legend>
        <div class="layui-field-box layui-form">
            <table class="layui-table admin-table">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="pager">
                </tbody>
            </table>
        </div>
    </fieldset>
    <div class="page-table-box" id="page" style=" width:1000px; margin:0 auto; margin-top:100px;"></div>
</div>
</body>

<script type="text/javascript">

    $(function () {
        pageList(1);
    });

    function pageList(pageNum) {

        $.ajax({
            url: "/admin/city/",
            type: "get",
            dataType: "json",
            data: {"pageNum": pageNum, "pageSize": window.page.pageSize},
            success: function (r) {

                console.log(r);

                if (r.code == -1) {
                    return;
                }

                page(pageNum, r.data.total);

                $("#pager").html("");

                var str = "";

                $.each(r.data.list, function (n, value) {
                    str += '<tr>';
                    str += '<td>' + value.name + '</td>';
                    str += '<td>';
                    str += '<a onclick="info(' + value.id + ')" class="layui-btn layui-btn-normal layui-btn-mini">查看支行</a>';
                    str += '<a onclick="edit(' + value.id + ')" class="layui-btn layui-btn-normal layui-btn-mini">修改</a>';
                    str += '<a onclick="del(' + value.id + ')" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>';
                    str += '</td>';
                    str += '</tr>';
                });

                $("#pager").html(str);
            }
        });
    }


    var layer;
    layui.use('layer', function () {
        layer = layui.layer;
    });

    $("#add").on("click", function () {
        layer.open({
            type: 2,
            title: '添加产品',
            shadeClose: true,
            shade: false,
            zIndex: 99999999,
            maxmin: true, //开启最大化最小化按钮
            area: ['600px', '200px'],
            content: '/admin/city/add',
            btn: ['确定', '取消'],
            yes: function (index, layero) {

                var body = layer.getChildFrame('body',index);
                var validate = body.find("#validate").validator();
                var name = body.find("#name").val();

                if(!validate.check()){return;}

                $.ajax({
                    url: "/admin/city/",
                    type: "post",
                    dataType: "json",
                    data: {"name": name},
                    success: function (r) {
                        console.log(r);

                        if(r.code == -1){
                            layer.msg(r.msg);
                            return;
                        }
                        layer.close(index);
                        layer.msg('添加成功!',{time:1000}, function(){
                            //关闭后的操作
                            location.reload();
                        });
                    }
                });

            }
        });
    });

    function edit(id) {
        layer.open({
            type: 2,
            title: '修改产品',
            shadeClose: true,
            shade: false,
            zIndex: 99999999,
            maxmin: true, //开启最大化最小化按钮
            area: ['600px', '200px'],
            content: '/admin/city/edit/' + id,
            btn: ['确定', '取消'],
            yes: function (index, layero) {

                var body = layer.getChildFrame('body',index);
                var validate = body.find("#validate").validator();
                var id = body.find("#id").val();
                var name = body.find("#name").val();

                if(!validate.check()){return;}

                $.ajax({
                    url: "/admin/city/",
                    type: "put",
                    dataType: "json",
                    data: {"id":id,"name": name},
                    success: function (r) {
                        console.log(r);

                        if(r.code == -1){
                            layer.msg(r.msg);
                            return;
                        }
                        layer.close(index);
                        layer.msg('修改成功!',{time:1000}, function(){
                            //关闭后的操作
                            location.reload();
                        });
                    }
                });

            }
        });
    }

    function del(id) {
        //询问框
        layer.confirm('确定删除?', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url: "/admin/city/"+id,
                type: "delete",
                dataType: "json",
                success: function (r) {
                    console.log(r);

                    if(r.code == -1){
                        layer.msg(r.msg);
                        return;
                    }
                    layer.msg('删除成功!',{time:1000}, function(){
                        //关闭后的操作
                        location.reload();
                    });
                }
            });
        });
    }
    
    function info(id) {
        layer.open({
            type: 2,
            title: '查看比例',
            shadeClose: true,
            shade: false,
            zIndex: 99999999,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '500px'],
            content: '/admin/addr/list/' + id
        });
    }

</script>

</html>